//
//  ZLCalendarSaleDetailScrollView.swift
//  TestDemo
//
//  Created by ayin on 2019/5/29.
//  Copyright © 2019 aYin. All rights reserved.
//  日历-发售详情-自定义scrollview

import UIKit

class ZLCalendarSaleDetailScrollView: UIScrollView, UIScrollViewDelegate, UIGestureRecognizerDelegate {
    //MARK: - 声明属性
    var canMove: Bool = true
    var onlyCanMove : Bool = false
    
    //MARK: - 初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.delegate = self
        NotificationCenter.default.addObserver(self, selector: #selector(contentScrollCanMove(notification:)), name: ZLCalendarNotifiContentScrollCanMoveKey, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(contentScrollOnlyCanMove(notification:)), name: ZLCalendarNotifiContentScrollOnly, object: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    //MARK: - 事件
    //通知事件
    @objc func contentScrollCanMove(notification: NSNotification) {
        canMove = true
    }
    
    @objc func contentScrollOnlyCanMove(notification: NSNotification) {
        onlyCanMove = true
    }
    
    //MARK: - 代理
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let maxOffsetY: CGFloat = 180.up
        let offsetY: CGFloat = scrollView.contentOffset.y
        
        if onlyCanMove {
            return
        }
        
        if !canMove {
            scrollView.contentOffset = CGPoint(x: 0, y: maxOffsetY)
            return
        }
        
        if offsetY >= maxOffsetY {
            NotificationCenter.default.post(name: ZLCalendarNotifiSubTableCanMoveKey, object: nil)
            canMove = false
        }
        
        NotificationCenter.default.post(name: ZLCalendarNotifiContentScrollOffsetYKey, object: nil, userInfo: ["offsetY": offsetY])
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}
